package com.gxa.meiye.portal.shop.shopLogin.controller;

import com.gxa.meiye.core.common.ResponseData;
import com.gxa.meiye.core.pojo.shopManagement.Shop;
import com.gxa.meiye.core.utils.RandomUtils;
import com.gxa.meiye.core.utils.RestUtils;
import com.gxa.meiye.portal.shop.shopLogin.service.GetBackPwdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author cc
 * @PackageName meiye
 * @Package com.gxa.portal.shop.shopLogin.controller
 * @Date 2022/7/26 9:36
 * @Version 1.0
 */
@RestController
@Api(tags = "找回密码接口")
public class PortalGetBackPwdController {
   @Autowired
    private GetBackPwdService getBackPwdService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @PostMapping("getBackPwdEmail")
    @ApiOperation("发送找回密码的验证码")
    public ResponseData getBackYouPwd(String getBackPwdEmail){
        ResponseData rd = new ResponseData();
        String getBackPwdCode = RandomUtils.getSixBitRandom();
        getBackPwdService.send(getBackPwdEmail,getBackPwdCode);
        ValueOperations<String, String> getBackPwdEmailAndCode= redisTemplate.opsForValue();
        getBackPwdEmailAndCode.set(getBackPwdEmail,getBackPwdCode,5, TimeUnit.MINUTES);
        if(redisTemplate.hasKey(getBackPwdEmail)){
            rd.setCode(200);
            rd.setMsg("发送验证码成功");
        }else{
            rd.setCode(500);
            rd.setMsg("系统开小差，请联系管理员！");
        }
        return rd;
    }

    @PostMapping("setNewPwd")
    @ApiOperation("设置新密码")
    public ResponseData setNewPwd(Shop shop){
        ResponseData rd = new ResponseData();
        String getBackPwdEmail = shop.getGetBackPwdEmail();
        String getBackPwdCode = shop.getGetBackPwdCode();
        Boolean hasKey = redisTemplate.hasKey(getBackPwdEmail);
        if(getBackPwdCode.equals("")||getBackPwdCode.equals(null)){
            rd.setMsg("请先获取验证码");
        }else if(!hasKey){
            rd.setMsg("验证码已失效");
        } else if(!(redisTemplate.opsForValue().get(getBackPwdEmail).equals(getBackPwdCode))){
            rd.setMsg("验证码错误");
        }else if(shop.getNewPwd().equals("")||shop.getNewPwd().equals(null)){
            rd.setMsg("密码不能为空");
        }else if(!(shop.getNewPwd().equals(shop.getRepeatNewPwd()))) {
            rd.setMsg("两次输入的密码不一致");
        }else {
            /*通过远程调用保存到后台数据库*/
            String url = "http://1.117.60.194:8080/shop/setNewPwd";
            MultiValueMap<String,Object> params = new LinkedMultiValueMap<>();
            params.add("pwd",shop.getNewPwd());
            params.add("email",shop.getGetBackPwdEmail());
            RestUtils.postWithParams(url, params);
            rd.setMsg("重设密码成功");
            rd.setCode(200);
            redisTemplate.delete(getBackPwdEmail);
        }
        return rd;
    }

}
